//============================================================================
// Name        : audiokck.cpp
// Author      : Tomasz Lipert
// Version     : 1.1
// Description :
//============================================================================

#include <iostream>
#include <SDL/SDL_audio.h>
#include <cmath>
#include <SDL/SDL.h>
#include <SDL/SDL_draw.h>


using namespace std;


int WIDTH = 1200;
int HEIGHT = 600;
int window = 25000;
int offset = 5000;

Uint32 colors[1024];

int main() {

	SDL_Surface *screen = NULL;
	SDL_Init(SDL_INIT_EVERYTHING);
	screen = SDL_SetVideoMode(WIDTH, HEIGHT, 32, SDL_SWSURFACE);

	SDL_AudioSpec wav_spec;
	Uint32 wav_length;
	Uint8 *wav_buffer;


	int N = 256;
	for (int i = 0; i < 256; i++) {
		colors[i] = SDL_MapRGB(screen->format, 0, i, 255);
		//colors[i] = SDL_MapRGB(screen->format, 255, i, 0);
	}

	for (int i = 0; i < 256; i++) {
		colors[256 + i] = SDL_MapRGB(screen->format, 0, 255, 255 - i);
		//colors[256 + i] = SDL_MapRGB(screen->format, 255 - i, 255, 0);
	}

	for (int i = 0; i < 256; i++) {
		colors[512 + i] = SDL_MapRGB(screen->format, i, 255, 0);
		//colors[512 + i] = SDL_MapRGB(screen->format, 0, 255, i);
	}

	for (int i = 0; i < 256; i++) {
		colors[768 + i] = SDL_MapRGB(screen->format, 255, 255 - i, 0);
		//colors[768 + i] = SDL_MapRGB(screen->format, 0, 255 - i, 255);
	}

	/* open audio */
	if (SDL_LoadWAV("m4.wav", &wav_spec, &wav_buffer, &wav_length) == NULL) {
		fprintf(stderr, "Could not open test.wav: %s\n", SDL_GetError());
		exit(-1);
	}

	Sint16* samples = (Sint16*) (wav_buffer);



	cout << "sample rate: " << wav_spec.freq << endl;
	cout << "length: " << wav_length/2 << endl;

	double* intable = new double[window+offset];
	double* intable2 = new double[window];
	double* result = new double[offset];
	intable2[0] = 1;
	intable2[1] = 2;
	intable2[2] = 3;
	intable2[3] = 4;
	intable2[4] = 5;
	for(int i = 0; i < window; i++){
		intable[i] = samples[i];
	}



for (int i = 0; i <= offset; i++){
	for(int j = 0; j < window; j++){
			intable2[j] = samples[j+i];
			//cout << "intable2[" << j << "]: " << intable2[j] << endl;
			//cout << "samples[" << j << "]: " << intable2[j+i] << endl;
	}
	//for(int i =0; i < 1000; i++){
	//					cout << "in2["<<i<<"]: "<<samples[i] << endl;
	//				}

	double sum1 = 0;
	double sum2 = 0;
	double sumsq1 = 0;
	double sumsq2 = 0;
	double summul = 0;

	for (int i = 1; i <= window; i++){
		sum1 += intable[i];
	}
//cout << "sum1:" << sum1 << endl;
	for (int i = 1; i <= window; i++){
			sum2 += intable2[i];
	}
	//cout << "sum2:" << sum2 << endl;
	for (int i = 1; i <= window; i++){
			sumsq1 += (intable[i]*intable[i]);
	}

	for (int i = 1; i <= window; i++){
			sumsq2 += (intable2[i]*intable2[i]);
	}

	for (int i = 1; i <= window; i++){
			summul += (intable[i]*intable2[i]);
	}

	result[i] = (window*summul-sum1*sum2)/(sqrt(window*sumsq1-pow(sum1,2))*sqrt(window*sumsq2-pow(sum2,2)));

}
for (int i = 0; i <= offset; i++){
	cout << i << ": " << result[i] << endl;
}
int count = 0;
double found;
for(int i = 7; i < offset; i++){
	if(result[i] > result[i+1] && result[i] > result[i-1]){
		found=i;
		break;
		//count++;
	}
}
//found = offset/(double)(count+1);
cout << "Found: " << found<<endl;
//found = 7;
double wynik = 1/(found/wav_spec.freq*(wav_length/2)/wav_spec.freq);

cout << "Wynik: " << 2*wynik;

for (int i = 0; i < 1024; i++) {
			Draw_Pixel(screen, 10 + i, 200,	colors[400]);
			///cout << (int)(result[i] / divider) << endl;
		}

for (int i = 1; i < 1000; i++) {
			Draw_Pixel(screen, 10 + i, 200+100*result[5*i],	colors[200]);
			///cout << (int)(result[i] / divider) << endl;
		}


	SDL_Flip(screen);

	SDL_Delay(10000);
	SDL_Quit();

	return 0;
}
